package udp;

import java.net.*;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

public class SendByUDP {
    public static void main(String[] args) throws Exception {
        //创建连接对象
        DatagramSocket datagramSocket=new DatagramSocket();

        Scanner scanner=new Scanner(System.in);
        InetAddress localhost = InetAddress.getByName("localhost");
        //处理发送数据
        while (true){
            System.out.println("请输入发送的消息,输入886退出聊天");
            String s=scanner.next();
            if("886".equals(s)){
                System.out.println("退出");
                break;
            }
            byte[] bytes = s.getBytes();
            DatagramPacket datagramPacket=new DatagramPacket(bytes,bytes.length,localhost,10088);
            //发送数据
            datagramSocket.send(datagramPacket);
        }
        //关闭资源
        datagramSocket.close();

    }
}
